// 判断用户是否登录
const user = localStorage.getItem('login_user');



// 关键字可以为空
let kw = '';

// 排序的字段
// let sortName = 'id'

// 排序的方法  asc / desc
let sortType = '';
let page = 1;
let showSize = 16;
let pageTotal = 0;
let sortName = ''; //手机 电视

async function pageload() {
    const res = await goodList({
        page,
        showSize
    })
    getDate(res)
}
pageload()

// console.log($('.serch_btn'));



//搜索
$('.serch_btn').onclick = async function () {
    kw = $('.serch_con').value;
    const res = await goodList({
        kw,
        page,
        showSize,
        sortType
    })
    console.log(res);
    getDate(res)
}

// // 升序
$('.asc').onclick = async function () {
    const res = await goodList({
        kw,
        page,
        showSize,
        sortType: "asc",
        sortName
    })

    getDate(res)
}


// 降序
$('.desc').onclick = async function () {
    const res = await goodList({
        kw,
        page,
        showSize,
        sortType: "desc",
        sortName
    })

    getDate(res)
}

//分类
$('.class').onclick = async function (e) {
    const target = e.target
    if (target.tagName === 'SPAN') {
        page = 1;
        sortName = target.innerHTML;
        const res = await goodList({
            kw,
            page,
            showSize,
            sortType: "desc",
            sortName
        })
        getDate(res)
    } else {
        alert(00)
    }
}


//上翻页
$('.prev').onclick = async function () {
    if (!this.className.includes('dis')) {
        page--;
        const res = await goodList({
            page,
            showSize,
            sortType: "asc",
            sortName
        })

        getDate(res)

        if (this.nextElementSibling.nextElementSibling.className.includes('dis')) {
            this.nextElementSibling.nextElementSibling.classList.remove('dis')
        }
        if (page === 1) {
            this.classList.add('dis')
        }
    }
}

//下翻页
$('.next').onclick = async function () {
    if (!this.className.includes('dis')) {
        page++;
        const res = await goodList({
            page,
            showSize,
            sortType: "asc",
            sortName
        })

        getDate(res)

        if (this.previousElementSibling.previousElementSibling.className.includes('dis')) {
            this.previousElementSibling.previousElementSibling.classList.remove('dis')
        }
        if (page === pageTotal) {
            this.classList.add('dis')
        }
    }
}


//加入购物车
$('.list_mi').onclick = async function (e) {
    const target = e.target
    if (target.tagName === 'BUTTON') {
        if (user) {
            const goods_id = target.parentNode.getAttribute('data-id')
            const goods_price = target.parentNode.getAttribute('data-price')
            console.log(goods_id, goods_price);

            const res = await addToCart({
                username: user,
                goods_id,
                num: 1,
                goods_price
            })
            console.log(res);
            const {
                status,
                msg,
                data
            } = res
            if (status) {
                alert("成功加入购物车");
            } else {
                alert(msg);
            }


            // getDate(res)
        } else {
            alert("您还没有登录")
            location.href = './login.html'
        }
    }
}




async function getDate(res = '') {
    const {
        status,
        msg,
        data,
        pageAll
    } = res
    // console.log(res);


    if (status) {
        let str = '';
        data.forEach(v => {
            str += `
            <div class="goods_item" data-id="${v.id}" data-price="${v.price}">
            <img src="${v.img}" alt="">
            <h2>${v.title}</h2>
            <p>${v.price}元</p>
            <button><i class="iconfont icon-gouwucheman"></i></button>
        </div>
            `
        })
        $('.list_mi').innerHTML = str;

        pageTotal = Math.ceil(pageAll / showSize);

        if (pageTotal == 1) {
            $('.next').classList.add('dis')
        }
        $('.pageAll').innerHTML = page + '/' + pageTotal


    } else {
        alert(msg)
    }
}